const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    Base: '',
    current_time: 0,
    discount_adv: {},
    goods_category_list: {},
    goods_list: {},
    category_id: 0,
    page: 1,
    timer_array: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let category_id = options.category_id;
    let base = app.globalData.siteBaseUrl;

    if (category_id != undefined){
      that.setData({
        category_id: category_id
      })
    }
    that.setData({
      Base: base
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    app.sendRequest({
      url: 'api.php?s=index/getDiscountData',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {

        let code = res.code;
        if (code == 0) {
          let data = res.data;
          that.setData({
            current_time: data.current_time,
            discount_adv: data.discount_adv,
            goods_category_list: data.goods_category_list
          })
          that.getGoodsList(that);
        }
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 获取折扣商品
   */
  getGoodsList: function (that){
    let category_id = that.data.category_id;
    let page = page;

    app.sendRequest({
      url: 'api.php?s=index/getDiscountGoods',
      data: {
        category_id: category_id,
        page: page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          let goods_list = res.data.data;
          let timer_array = {};
          for (let index in goods_list){
            timer_array[index] = {};
            timer_array[index].end_time = goods_list[index].end_time;
          }
          for (let key in timer_array){
            that.timing(that, timer_array, key);
          }
          that.setData({
            goods_list: goods_list
          })
          console.log(res)
        }
      }
    });
  },

  /**
   * 计时
   */
  timing: function (that, timer_array, index){
    let current_time = that.data.current_time;
    let count_second = (timer_array[index].end_time*1000 - current_time)/1000;
    if (count_second > 0) {
      count_second--;
      let day = Math.floor((count_second / 3600) / 24);
      let hour = Math.floor((count_second / 3600) % 24);
      let minute = Math.floor((count_second / 60) % 60);
      let second = Math.floor(count_second % 60);
      timer_array[index].nowtime = day + '天' + hour + '小时' + minute + '分' + second + '秒';

      that.setData({
        timer_array: timer_array
      })
    } else {
      timer_array[index].nowtime = '活动已结束 !';

      that.setData({
        timer_array: timer_array
      })
    }
    let timer = setInterval(function(){
      if (count_second > 0){
        count_second--;
        let day = Math.floor((count_second / 3600) / 24);
        let hour = Math.floor((count_second / 3600) % 24);
        let minute = Math.floor((count_second / 60) % 60);
        let second = Math.floor(count_second % 60);
        timer_array[index].nowtime = day+'天'+hour+'小时'+minute+'分'+second+'秒';

        that.setData({
          timer_array: timer_array
        })
      }else{
        timer_array[index].nowtime = '活动已结束 !';

        that.setData({
          timer_array: timer_array
        })
        clearInterval(timer);
      }
    },1000)
  },
  
  /**
   * 商品详情
   */
  aClick: function (event) {
    let url = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages'+url,
    })
  }
})